<template>
	<view>
		<u-popup :show="showpaymodel" mode="bottom" :round="12" @close="btnclose()">
			<view class="showmodel" style="height: 500rpx;">
				<view class="showmodel_paytop">
					<view class="payname"></view>
					<view class="payname">选择支付方式</view>
					<image src="../../static/guanbi.a2efebd2.png" @click="btnclose()" mode="">
					</image>
				</view>
				<view class="showmodel_paylsit" v-for="(item,index) in payTypeList" :key="index"
					@click="changePayType(item)">
					<image :src="$fun.imgUrl(item.image)" mode=""></image>
					<view class="mleft">
						<view class="">{{item.name}}</view>
						<view class="ism">{{item.money}}</view>
						<view class="rtop" v-if="Number(item.money)<Number(money)||item.money===0">余额不足</view>
					</view>
				</view>
			</view>
		</u-popup>
		<u-overlay :show="showpaw">
			<view class="showpaw">
				<view class="text">输入交易密码</view>
				<u-code-input :value="payvalue" :focus="true" :dot="true" size='32' @change="payfinish" fontSize='14'
					color='#333333' space='5' :maxlength="6"></u-code-input>
				<view class="btn flex-between">
					<view class="btn_close" @click="showpaw=false">取消</view>
					<view class="btn_sure" @click="complete()">确定</view>
				</view>
			</view>
		</u-overlay>
	</view>
</template>

<script>
	export default {
		props: {
			oid: {
				type: String,
				default: ""
			},
			type: {
				type: String,
				default: ""
			},
			showpaymodel: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				payType: "",
				orderInfo: {},
				// 支付方式
				payTypeList: [],
				showBoard: false,
				payMoney: 0,
				money: 0,
				showpaw: false,
				payvalue: '' //支付密码
			}
		},
		onLoad() {
			this.getorder()
		},
		methods: {
			btnclose() {
				this.showpaymodel = false
				this.zIndex = 10075
				this.$emit('showpaybtn', this.showpaymodel)
			},
			// 获取订单
			getorder(oid) {
				this.$fun.ajax.post('order/orderpay', {
					oid
				}).then(res => {
					if (res.code == 1) {
						this.payTypeList = res.data.pay
						this.money = res.data.money
						// this.payType = res.data.pay[0].code
						// this.payMoney = res.data.pay[0].money
					}
				})
			},
			//选择支付方式
			changePayType(item) {
				if (Number(item.money) < Number(this.money) || this.money === 0) {
					uni.showToast({
						title: '余额不足',
						icon: 'none',
						duration: 1000
					})
					if (this.type == '1') {
						setTimeout(() => {
							this.$fun.jump(`/pageA/order/order?state=0`, 2)
						}, 1200)
					}
					return;
				}
				this.payType = item.code;
				console.log(this.payType)
				this.btnclose()
				this.handleModel()
			},
			handleModel() {
				//console.log(this.payType)
				if (this.payType == 'wechat' || this.payType == 'alipay') {
					this.confirm()
				} else {
					this.showpaw = !this.showpaw
				}
			},
			payfinish(value) {
				this.payvalue = value
			},
			complete() {
				uni.showLoading({
					title: '',
				})
				this.$fun.ajax.post('user/setPay', {
					paypwd: this.payvalue
				}).then(res => {
					// this.$refs.payJP.password = ''
					this.payvalue = ''
					if (res.code == 1) {
						this.showpaw = false
						setTimeout(() => {
							uni.hideLoading()
						}, 1000)
						this.confirm()
					}
				})
			},
			wxpay(data) {
				var vm = this
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', { // 下面参数内容都是后台返回的
						'appId': data.appId, // 公众号名称，由商户传入
						'timeStamp': data.timeStamp, // 时间戳
						'nonceStr': data.nonceStr, // 随机串
						'package': data.package, // 预支付id
						'signType': data.signType, // 微信签名方式
						'paySign': data.paySign // 微信签名
					},
					function(res) {
						// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						if (res.err_msg === 'get_brand_wcpay_request:ok') {
							vm.$fun.msg('支付成功')
							uni.redirectTo({
								url: '/pageB/paySuccess?status=1'
							})
						} else {
							vm.$fun.msg('支付失败')
						}
					}
				)
			},
			//确认支付
			async confirm() {
				console.log(this.oid)
				let vm = this;
				await this.$fun.ajax.post('order/pay', {
					oid: this.oid,
					type: this.payType
				}).then(res => {
					let data = res.data
					if (res.code == 1) {
						if (this.payType == 'wechat') {
							if (typeof WeixinJSBridge === 'undefined') { // 微信浏览器内置对象。参考微信官方文档
								if (document.addEventListener) {
									document.addEventListener('WeixinJSBridgeReady', vm.wxpay(data), false)
								} else if (document.attachEvent) {
									document.attachEvent('WeixinJSBridgeReady', vm.wxpay(data))
									document.attachEvent('onWeixinJSBridgeReady', vm.wxpay(data))
								}
							} else {
								vm.wxpay(data)
							}
							return
						} else if (this.payType == 'alipay') {
							uni.requestPayment({
								//服务提供商 通过uni.getProvider获取
								provider: 'alipay',
								//后台返回的订单数据
								orderInfo: res.data,
								//调用成功的回调
								success(res) {
									this.$fun.msg('支付成功')
									uni.redirectTo({
										url: './payback?status=1'
									})
								},
								//调用失败的回调
								fail(err) {
									console.log(err)
									console.log('fail:' + err);
								}
							})
							return
						}
						uni.redirectTo({
							url: './payback?status=1'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
						uni.redirectTo({
							url: `./payback?status=2&oid=${this.oid}`
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.showmodel {
		width: 750rpx;
		max-height: 1152rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 0rpx 0rpx;

		&_top {
			display: flex;
			justify-content: space-between;
			margin: 30rpx;



			.left {
				font-weight: 600;
				font-size: 48rpx;
				color: #ED4961;
				display: flex;
				align-items: center;

				text {
					font-size: 28rpx;
				}

				image {
					width: 120rpx;
					height: 120rpx;
					margin-right: 20rpx;
				}
			}

			image {
				width: 48rpx;
				height: 48rpx;
			}
		}

		&_paytop {
			width: 750rpx;
			height: 88rpx;
			border-radius: 1rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx;

			image {
				width: 48rpx;
				height: 48rpx;
			}

			.payname {
				font-weight: 600;
				font-size: 32rpx;
				color: #19181A;
			}
		}

		&_paylsit {
			width: 690rpx;
			height: 100rpx;
			background: #F6F6F6;
			border-radius: 24rpx;
			display: flex;
			align-items: center;
			font-weight: 400;
			font-size: 28rpx;
			color: #19181A;
			margin: 30rpx 30rpx;
			padding-left: 22rpx;
			position: relative;

			.rtop {
				width: 100rpx;
				height: 28rpx;
				line-height: 28rpx;
				text-align: center;
				background: #CCCCCC;
				border-radius: 10rpx;
				font-weight: 400;
				font-size: 20rpx;
				color: #FFFFFF;
				position: absolute;
				top: -14rpx;
				right: 0;
			}

			image {
				width: 42rpx;
				height: 42rpx;
				margin-right: 22rpx;
			}

			.mleft {
				display: flex;
				align-items: center;

				.ism {
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					margin-left: 10rpx;
				}
			}
		}

		.isline {
			width: 750rpx;
			height: 16rpx;
			background: #F5F1EE;
		}

		&_address {
			display: flex;
			height: 156rpx;
			justify-content: space-between;
			align-items: center;
			margin: 0 30rpx;

			.left {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
			}

			.right {
				font-weight: 400;
				font-size: 24rpx;
				color: #333333;
				display: flex;
				align-items: center;

				image {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}

		&_num {
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
			display: flex;
			justify-content: space-between;
			margin: 40rpx 30rpx;
		}

		&_size {
			margin: 30rpx;

			.name {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
			}

			.slist {
				display: flex;
				align-items: center;
				padding: 6rpx 0;
				margin-top: 20rpx;
				position: relative;

				.rtop {
					font-weight: 400;
					font-size: 20rpx;
					color: #FFFFFF;
					width: 60rpx;
					height: 28rpx;
					line-height: 28rpx;
					text-align: center;
					background: #CCCCCC;
					border-radius: 10rpx;
					position: absolute;
					top: -14rpx;
					right: 0;
				}

				image {
					width: 52rpx;
					height: 52rpx;
					margin-left: 6rpx;
					margin-right: 12rpx;

				}

				width: 690rpx;
				background: #F6F6F6;
				border-radius: 16rpx;
				font-weight: 400;
				font-size: 24rpx;
				color: #333333;
			}
		}


		.isbtn {
			position: fixed;
			bottom: 0;
			width: 750rpx;
			height: 98rpx;
			background: #FFFFFF;
			box-sizing: border-box;
			padding: 0 30rpx;

			.ispay {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.left {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				line-height: 28rpx;
				display: flex;
				align-items: center;

				.num {
					font-weight: 600;
					font-size: 48rpx;
					color: #ED4961;
					margin-left: 10rpx;

					text {
						font-size: 28rpx;
					}
				}
			}

			.rbtn {
				width: 360rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				background: #FB6C04;
				border-radius: 36rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
			}

			.radd {
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
				width: 690rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				background: #FB6C04;
				border-radius: 36rpx;
			}

		}
	}

	.showpaw {
		// width: 500rpx;
		height: 316rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		position: fixed;
		top: 30%;
		left: 18.5%;
		box-sizing: border-box;
		padding: 40rpx 30rpx;

		.text {
			font-weight: 500;
			font-size: 30rpx;
			color: #333333;
			text-align: center;
			margin-bottom: 30rpx;
		}

		.btn {
			margin-top: 40rpx;
			display: flex;
			justify-content: space-between;

			&_close {
				width: 186rpx;
				height: 72rpx;
				border-radius: 40rpx;
				border: 2rpx solid #0066FF;
				font-weight: 500;
				font-size: 26rpx;
				color: #0066FF;
				line-height: 72rpx;
				text-align: center;
			}

			&_sure {
				width: 186rpx;
				height: 72rpx;
				background: #0066FF;
				border-radius: 40rpx;
				font-weight: 500;
				font-size: 26rpx;
				color: #FFFFFF;
				line-height: 72rpx;
				text-align: center;
			}
		}
	}
</style>